package com.hq.fxh.No3_TuiGuang.Fragment;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v7.widget.AppCompatRadioButton;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hq.fxh.CustomerView.NoScrollViewPager;
import com.hq.fxh.No0_ChengQu.Fragment.BaseFragment;
import com.hq.fxh.No3_TuiGuang.Adaoter.RGStaticsAdapter;
import com.hq.fxh.No5_User.MyModle.AdEditorActivity;
import com.hq.fxh.R;

import java.util.ArrayList;
import java.util.List;


/**
 * @author 小谢
 * @ClassName BusinessPromotionFragment
 * @Description :企业账号推广页面
 * @Created 2016/12/27 0027
 */
public class BusinessPromotionFragment extends BaseFragment implements View.OnClickListener {
    protected TextView muban;
    protected RelativeLayout rl;
     private RadioGroup stati;
    private AppCompatRadioButton stati_persion;
    private AppCompatRadioButton stati_business;
    private int way = 0;
    private NoScrollViewPager vpStatisticSum;
    private List<Fragment> fragmentLists;
    private RGStaticsAdapter rGStaticsAdapter;
    private AppCompatRadioButton rbTuiGuang;
    private View container_ll;
    private RelativeLayout che;
    private RelativeLayout tablayout;
    private int SCROLL_LENGTH = 300;
    private ImageView back;
    private BusiPromotionFragment busiPromotionFragment;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_business_statistic_pro;
    }

    @Override
    protected void findView(View view) {
        stati_persion = (AppCompatRadioButton) rootView.findViewById(R.id.stati_persion);
        stati_business = (AppCompatRadioButton) rootView.findViewById(R.id.stati_business);
        rbTuiGuang = (AppCompatRadioButton) rootView.findViewById(R.id.rbTuiGuang);
        stati = (RadioGroup) rootView.findViewById(R.id.stati);
        muban = (TextView) rootView.findViewById(R.id.muban);
        muban.setOnClickListener(BusinessPromotionFragment.this);
        rl = (RelativeLayout) rootView.findViewById(R.id.rl);
        vpStatisticSum = (NoScrollViewPager) rootView.findViewById(R.id.vpStatisticSum);
        back = (ImageView) rootView.findViewById(R.id.img_back);
        che = (RelativeLayout) rootView.findViewById(R.id.che);
        tablayout = (RelativeLayout) rootView.findViewById(R.id.tablayout);
        container_ll =  rootView.findViewById(R.id.container_ll);
    }

    @Override
    protected void initData() {
        fragmentLists = new ArrayList<>();
        fragmentLists.add(new PromotionFragment());
        busiPromotionFragment = new BusiPromotionFragment();
        fragmentLists.add(busiPromotionFragment);
        //    fragmentLists.add(new InterPromotionFragment());
        rGStaticsAdapter = new RGStaticsAdapter(getChildFragmentManager(), fragmentLists);
        vpStatisticSum.setAdapter(rGStaticsAdapter);
        vpStatisticSum.setCurrentItem(1);
        registerListener();
        swichTab(1);
    }

    public void setVisible(RelativeLayout rl_tl, RelativeLayout rl_rg, int height) {

        if (height > 0) {
            container_ll.setVisibility(View.VISIBLE);//员工排行Title
            if (height <= SCROLL_LENGTH) {
                float alpha = height * 1f / SCROLL_LENGTH;
                tablayout.setAlpha(alpha);
                che.setAlpha(alpha);
            } else {
                tablayout.setAlpha(1);
                che.setAlpha(1);
            }

            if (rl_tl.getChildCount() > 0) {
                View childAt = rl_tl.getChildAt(0);
                rl_tl.removeView(childAt);
                tablayout.addView(childAt);
            }
            if (rl_rg.getChildCount() > 0) {
                RadioGroup childAt = (RadioGroup) rl_rg.getChildAt(0);
                rl_rg.removeView(childAt);
                che.addView(childAt);
                final RadioButton rbLeft = (RadioButton) childAt.getChildAt(0);
                rbLeft.setBackgroundResource(R.drawable.bg_rb_my_half_left);//选中背景为白色
                rbLeft.setTextColor(getResources().getColorStateList(R.color.my_color));
                final RadioButton rbReight = (RadioButton) childAt.getChildAt(1);
                rbReight.setBackgroundResource(R.drawable.bg_rb_my_half_right);
                rbReight.setTextColor(getResources().getColorStateList(R.color.my_color));
            }
        } else {
            container_ll.setVisibility(View.GONE);
            if (height >= -SCROLL_LENGTH) {
                float alpha = -height * 1f / SCROLL_LENGTH;
                stati.setAlpha(alpha);
            } else {
                stati.setAlpha(1);
            }

            if (tablayout.getChildCount() > 0) {
                View childAt = tablayout.getChildAt(0);
                tablayout.removeView(childAt);
                rl_tl.addView(childAt);
            }
            if (che.getChildCount() > 0) {
                RadioGroup childAt = (RadioGroup) che.getChildAt(0);
                che.removeView(childAt);
                rl_rg.addView(childAt);

                final RadioButton rbLeft = (RadioButton) childAt.getChildAt(0);
                rbLeft.setBackgroundResource(R.drawable.bg_rb_main_half_left);//选中背景为白色
                final RadioButton rbReight = (RadioButton) childAt.getChildAt(1);
                rbReight.setBackgroundResource(R.drawable.bg_rb_main_half_right);
                rbLeft.setTextColor(getResources().getColorStateList(R.color.main_color));
                rbReight.setTextColor(getResources().getColorStateList(R.color.main_color));
            }
        }
    }

    private void registerListener() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                busiPromotionFragment.recycler.smoothScrollToPosition(0);

            }
        });
        stati.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                switch (checkedId) {
                    //个人
                    case R.id.stati_persion:
                        way = 0;
                        swichTab(way);
                        vpStatisticSum.setCurrentItem(0);
                        break;
                    //企业
                    case R.id.stati_business:
                        way = 1;
                        swichTab(way);
                        vpStatisticSum.setCurrentItem(1);
                        break;
                    case R.id.rbTuiGuang:
                        way = 2;
                        swichTab(way);
                        vpStatisticSum.setCurrentItem(2);
                        break;
                }

            }
        });
    }

    public void swichTab(int code) {
        switch (code) {
            case 0:
                /** 个人 */
                stati_persion.setTextColor(getResources().getColor(R.color.new_red));
                stati_business.setTextColor(getResources().getColor(R.color.white));
                rbTuiGuang.setTextColor(getResources().getColor(R.color.white));
                break;
            case 1:
                /** 企业 */
                stati_persion.setTextColor(getResources().getColor(R.color.white));
                stati_business.setTextColor(getResources().getColor(R.color.new_red));
                rbTuiGuang.setTextColor(getResources().getColor(R.color.white));
                break;
            case 2:
                stati_persion.setTextColor(getResources().getColor(R.color.white));
                stati_business.setTextColor(getResources().getColor(R.color.white));
                rbTuiGuang.setTextColor(getResources().getColor(R.color.new_red));
                break;
            default:
                swichTab(0);
                break;
        }
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.muban) {
            startActivity(new Intent(getContext(), AdEditorActivity.class));
        }
    }
}
